﻿Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports TfsSearchCode

Module Module1

    Sub Main(ByVal cmdArgs() As String)

        Dim iChannel As Integer = 7080

        ' See if there are any arguments.
        If cmdArgs.Length > 0 Then
            For argNum As Integer = 0 To UBound(cmdArgs, 1)
                cmdArgs(argNum) = cmdArgs(argNum).ToUpper
                If cmdArgs(argNum).Contains("CHANNEL:") Then
                    iChannel = Val(Mid(cmdArgs(argNum), cmdArgs(argNum).IndexOf("CHANNEL:") + 7))
                End If
            Next argNum
        End If

        ' Register a channel httpp (soap) for two types of instantating objects
        '
        ' Server activated type: Have two subtypes; single call or Singleton, the first create a new 
        ' instance for any method invoke of a class (like style web services) and the second 
        ' when the server create an instance share the same instance for all clients (like static
        ' object across clientes)
        '
        ' Client activated type: the object is instanting on server, but is passed to client, 
        ' each clienthave a particular instancve of object and determines the live of object
        ' by thechnical supports based on spondsor objects whos renews the life of objects.


        Console.WriteLine("Channel " & CStr(iChannel))
        Dim channel As New HttpChannel(iChannel)
        ChannelServices.RegisterChannel(channel, False)

        ''Client activated type *************************************************************
        RemotingConfiguration.ApplicationName = "TFSSearchCodeClass"
        RemotingConfiguration.RegisterActivatedServiceType(GetType(TFSSearchCodeClass))

        ''Server activated 'SingleCall' *************************************************************
        'Dim myWellKnownServiceTypeEntry As New WellKnownServiceTypeEntry(GetType(TFSSearchCodeClass), _
        '                                           "object1uri", WellKnownObjectMode.SingleCall)
        'RemotingConfiguration.RegisterWellKnownServiceType(myWellKnownServiceTypeEntry)


        'Server activated 'Singleton' *************************************************************
        'Dim myWellKnownServiceTypeEntry As New WellKnownServiceTypeEntry(GetType(TFSSearchCodeClass), _
        '                                           "object1uri", WellKnownObjectMode.Singleton)
        'RemotingConfiguration.RegisterWellKnownServiceType(myWellKnownServiceTypeEntry)


        'another way to create server shared instance *************************************************************
        'Dim object1 As New TFSSearchCodeClass()
        'Dim ref1 As ObjRef = RemotingServices.Marshal(object1, "object1uri")
        'Console.WriteLine("ObjRef.URI: " & ref1.URI)
        'Console.WriteLine("Running. Press Enter to end publication.")
        'Console.ReadLine()
        ' This unregisters the object from publication, but leaves
        ' the channel listening.
        '   RemotingServices.Disconnect(object1)
        'Console.WriteLine("Disconnected the object. Client now receives a RemotingException.")
        'Console.WriteLine("Press Enter to unregister the channel.")

        Console.WriteLine() '*************************************************************
        Console.ReadLine()
        ' At this point, the ServerClass object still exists. The server
        ' could republish it.

        ' This unregisters the channel, but leaves the application 
        ' domain running.
        ChannelServices.UnregisterChannel(channel)
        Console.WriteLine("Unregistered the channel. Client now receives a WebException.")
        ' The ServerClass object still exists. The server could
        ' reregister the channel and republish the object.
        Console.WriteLine("The host application domain is still running. Press Enter to stop the process.")
        Console.ReadLine()

        ' The ServiceClass object's Finalize method writes a message to
        ' the console. A single object will almost always succeed in 
        ' running its Finalize method before the Console is finalized;
        ' in a larger application, you could ensure that all objects 
        ' finalize before the application ends by calling the garbage 
        ' collector and waiting.
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub

End Module
